package com.sgzn.sgpay.service.impl;

import com.sgzn.sgpay.enums.SgsResponseCode;
import com.sgzn.sgpay.model.request.SgsPrivQueryRequest;
import com.sgzn.sgpay.model.request.SgsQueryPrivAndShopRequest;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.model.response.result.PrivShopQueryResult;
import com.sgzn.sgpay.model.response.vo.PrivilegeVo;
import com.sgzn.sgpay.model.response.vo.ShopVo;
import com.sgzn.sgpay.persistence.dao.TMerchantPrivMapper;
import com.sgzn.sgpay.persistence.dao.TPrivilegeMapper;
import com.sgzn.sgpay.persistence.domain.TMerchantPriv;
import com.sgzn.sgpay.persistence.domain.TMerchantPrivExample;
import com.sgzn.sgpay.persistence.domain.TPrivilege;
import com.sgzn.sgpay.persistence.domain.TPrivilegeExample;
import com.sgzn.sgpay.service.SgsPrivQueryService;
import com.sgzn.sgpay.service.support.SgsResponseUtil;
import com.sgzn.sgpay.service.support.SubMchManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限查询服务
 * @author zhongzhongyong
 * @version Id: SgsPrivQueryServiceImpl.java, v 0.1 2018/5/1 13:59 zhongzhongyong Exp $$
 */
@Slf4j
@Service
public class SgsPrivQueryServiceImpl implements SgsPrivQueryService {

    @Autowired
    private TMerchantPrivMapper tMerchantPrivMapper;

    @Autowired
    private  TPrivilegeMapper tPrivilegeMapper;

    @Autowired
    private SubMchManager subMchManager;

    /**
     * 根据商户ID查找权限列表
     * @param request
     * @return
     */
    @Override
    public SgsResultResponse<List<TPrivilege>> queryPrivByMchId(SgsPrivQueryRequest request) {
        try{
            TMerchantPrivExample example = new TMerchantPrivExample();
            TMerchantPrivExample.Criteria cra = example.createCriteria();
            cra.andMchIdEqualTo(request.getMchId());
            List<TMerchantPriv> privList = tMerchantPrivMapper.selectByExample(example);
            if(CollectionUtils.isEmpty(privList)){
                //返回空权限列表
                return SgsResponseUtil.INSTANCE.success(new ArrayList<TPrivilege>());
            }
            List<String> privIdList = new ArrayList<>();
            for(TMerchantPriv item : privList){
                privIdList.add(item.getPrivId());
            }
            //根据权限ID集合获取结果
            TPrivilegeExample example2 = new TPrivilegeExample();
            TPrivilegeExample.Criteria cra2 = example2.createCriteria();
            cra2.andPrivIdIn(privIdList);
            List<TPrivilege> resultList = tPrivilegeMapper.selectByExample(example2);
            //返回权限列表
            return SgsResponseUtil.INSTANCE.success(resultList);
        }catch (Exception e){
            log.error(" 查询权限异常：",e);
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C_DB_EXCEPTION.getResponseCode(),
                    SgsResponseCode.C_DB_EXCEPTION.getResponseDesc());
        }
    }

    /**
     * 根据商户token查找权限列表和门店列表
     * @param request
     * @return
     */
    @Override
    public SgsResultResponse<PrivShopQueryResult> queryPrivAndShopList(SgsQueryPrivAndShopRequest request) {
        try{
            //全部功能权限
            List<PrivilegeVo> allPrivList = subMchManager.getMainMchPrivList(request.getMchId(),
                    request.getLoginChannel(),request.getReqTime(),request.getInitiationID());

            //全部场地权限
            List<ShopVo> allShopList = subMchManager.getMainMchShopList(request.getLoginChannel(),request.getReqTime(),request.getInitiationID(), request.getMchId());

            PrivShopQueryResult result = new PrivShopQueryResult();
            result.setAllPrivList(allPrivList);
            result.setAllShopList(allShopList);
            return SgsResponseUtil.INSTANCE.success(result);
        }catch (Exception e){
            log.error("[{}]. 查找权限列表和门店列表.", request.getInitiationID(),e);
            return SgsResponseUtil.INSTANCE.fail(SgsResponseCode.C503000.getResponseCode(),
                    SgsResponseCode.C503000.getResponseDesc());
        }
    }

}
